import Vue from 'vue';
import Router from 'vue-router';
import NProgress from 'nprogress';
import profileRoutes from '@/router/profile';
import SigninRoutes from '@/router/signin';
import forwardRoutes from '@/router/forward';
import helpRoutes from '@/router/help';
import mineRoutes from '@/router/mine';
import displayRoutes from '@/router/display';
import * as dd from 'dingtalk-jsapi';

// 订单 & 支付
import orderDetail from '@/router/orderDetail';

// 预约分会场
import preserve from '@/router/preserve';

// 购票板块
import buyTicketHome from '@/router/buyTicket/buyTicketHome.vue'; //购票板块首页

// 用票人信息录入
import infoEntry from '@/router/infoEntry/infoEntry.vue'; // 用票人信息录入首页
import infoEntryGive from '@/router/infoEntry/infoEntryGive.vue'; // 用票人信息录入首页--关于转赠票的

// 订单填写
import orderEnter from '@/router/orderEnter/orderEnter.vue'; //订单填写

import dingtalk from './dingtalk';
import auth from './auth';

Vue.use(Router);
NProgress.configure({
  showSpinner: false
});

const router = new Router({
  routes: [{
      path: '/',
      redirect: '/mine'
    },
    ...profileRoutes,
    ...forwardRoutes,
    ...mineRoutes,
    ...SigninRoutes,
    ...helpRoutes,
    ...displayRoutes,
    {
      path: '/buyTicketHome',
      name: 'buyTicketHome',
      component: buyTicketHome,
      meta: {
        title: '购票',
        auth: false
      }
    },
    {
      path: '/orderEnter',
      name: 'orderEnter',
      component: orderEnter,
      meta: {
        title: '填写订单',
        auth: true
      }
    },
    {
      path: '/orderDetail',
      name: 'orderDetail',
      component: orderDetail,
      meta: {
        title: '订单详情',
        auth: true
      }
    },
    {
      path: '/preserve/:projectId',
      name: 'preserve',
      component: preserve,
      meta: {
        title: '预约分论坛',
        dingtalk: true
      }
    },
    {
      path: '/infoEntry',
      name: 'infoEntry',
      component: infoEntry,
      meta: {
        title: '用票人信息',
        auth: true
      }
    },
    {
      path: '/infoEntryGive',
      name: 'infoEntryGive',
      component: infoEntryGive,
      meta: {
        title: '用票人信息',
        auth: false
      }
    },
  ]
});

router.beforeEach(async (to, from, next) => {
  NProgress.start();
  const {
    title
  } = to.meta;

  if (typeof document !== 'undefined') document.title = title || 'UCAN';
  if (dd.env.platform !== 'notInDingTalk') {
    dd.ready(() => dd.biz.navigation.setTitle({
      title
    })); // 设置钉钉title
  }

  if (await auth(to, next, router, from)) return;
  if (await dingtalk(to, next, router, from)) return;

  next();
});

router.afterEach(() => {
  NProgress.done();
});

export default router;
